package com.pai4j.ugc.controller;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.domain.vo.request.request.ChattingRequestVO;
import com.pai4j.domain.vo.response.chatting.ChattingListResponseVO;
import com.pai4j.domain.vo.response.mess.ChatMessageResponseVO;
import com.pai4j.remote.ugc.ChatMessageClient;
import com.pai4j.ugc.messaegqueue.service.AbstractPersistentMessageService;
import com.pai4j.ugc.service.ChatMessageService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class ChatMessageController implements ChatMessageClient {


    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private AbstractPersistentMessageService persistentMessageService;

    /**
     * 查询用户对话列表
     *
     * @param userId        当前登录用户userId
     * @param lastChatTime  开始查询时间
     * @param size          最大获取对话列表条数
     * @return
     */
    @Override
    public PAIResponseBean<List<ChattingListResponseVO>> listChatting(
            @PathVariable String userId, Long lastChatTime, Integer size) {

        return PAIResponseBeanUtil.success(chatMessageService.listChatting(userId, lastChatTime, size));
    }

    @Override
    public PAIResponseBean<Boolean> addChattingList(@RequestBody @Valid ChattingRequestVO chattingRequest) {

        return PAIResponseBeanUtil.success(chatMessageService.addChattingList(chattingRequest));
    }

    @Override
    public PAIResponseBean<List<ChatMessageResponseVO>> list(String sourceAccount, String targetAccount, Integer size, Long lastId, Long fixedMessageId) {
        List<ChatMessageResponseVO> chatMessages= persistentMessageService.getChatMessListFromCache(sourceAccount, targetAccount, lastId, fixedMessageId, size);
        return PAIResponseBeanUtil.success(chatMessages);
    }

    @Override
    public PAIResponseBean<List<String>> query(String sourceUserId, String targetUserId,
                                               Long startTime, Long endTime, Integer count) {

        List<String> messages = persistentMessageService.query(sourceUserId, targetUserId, startTime, endTime, count);
        return PAIResponseBeanUtil.success(messages);
    }

    /**
     * 标记消息为已读
     * 前端在用户真正查看消息时主动调用，确保未读红点和未读数准确
     *
     * @param currUserId 当前用户ID（消息接收人）
     * @param chatUserId 聊天对象ID（消息发送人）
     * @return 标记已读的消息数量
     */
    @Override
    public PAIResponseBean<Integer> markMessagesAsRead(String currUserId, String chatUserId) {
        int count = persistentMessageService.markMessagesAsRead(currUserId, chatUserId);
        return PAIResponseBeanUtil.success(count);
    }

    /**
     * 查询未读消息数量
     *
     * @param currUserId 当前用户ID（消息接收人）
     * @param chatUserId 聊天对象ID（消息发送人）
     * @return 未读消息数量
     */
    @Override
    public PAIResponseBean<Long> getUnreadMessageCount(String currUserId, String chatUserId) {
        long count = persistentMessageService.getUnreadMessageCount(currUserId, chatUserId);
        return PAIResponseBeanUtil.success(count);
    }
}
